<?php

namespace TpAmis\Traits;

use think\facade\Cache;
use think\facade\Filesystem;
use think\facade\Request;

trait UploadTrait
{

    public function uploadImage()
    {
        return $this->upload('image');
    }


    public function uploadFile()
    {
        return $this->upload();
    }

    public function uploadRich()
    {
        $fromWangEditor = false;
        $file = Request::file('file');

        if (!$file) {
            $fromWangEditor = true;
            $file = Request::file('wangeditor-uploaded-image');
            if (!$file) {
                $file = Request::file('wangeditor-uploaded-video');
            }
        }

        if (!$file) {
            return $this->error('上传失败');
        }

        $path = Filesystem::disk('local')->putFile(config('amis.upload.directory.rich'), $file);

        $link = Filesystem::disk('local')->url($path);

        if ($fromWangEditor) {
            return $this->success(['url' => $link]);
        }

        return $this->success(compact('link', ['link' => $link]));
    }

    protected function upload($type = 'file')
    {
        $file = Request::file('file');

        if (!$file) {
            return $this->error('上传失败');
        }

        $path = Filesystem::disk('local')->putFile(config('amis.upload.directory.' . $type), $file);

        return $this->success(['value' => $path]);
    }

    //分片上传
    public function chunkUploadStart()
    {
        $uploadId = (string) $this->generateUUID();

        Cache::set($uploadId, [], 600);

        Filesystem::disk('local')->makeDir('chunk/' . $uploadId);

        return $this->success('开始上传', ['uploadId' => $uploadId]);
    }

    private function generateUUID()
    {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // 设置版本号为 4
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // 设置变体号为 1
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    //分片上传
    public function chunkUpload()
    {
        $uploadId = input('uploadId');
        $partNumber = input('partNumber');
        $file = request()->file('file');

        $path = 'chunk/' . $uploadId;

        $file->move(root_path('public/storage/' . $path), $partNumber);

        $eTag = md5(Filesystem::disk('local')->read($path . '/' . $partNumber));

        return $this->success('分片上传成功', ['eTag' => $eTag]);
    }
    //分片上传
    public function chunkUploadFinish()
    {
        $fileName = input('filename');
        $partList = input('partList');
        $uploadId = input('uploadId');
        $type = input('t', 'uploads');

        $ext = pathinfo($fileName, PATHINFO_EXTENSION);
        $path = $type . '/' . $uploadId . '.' . $ext;

        Filesystem::disk('local')->makeDir(dirname($path));

        for ($i = 0; $i < count($partList); $i++) {
            $partNumber = $partList[$i]['partNumber'];
            $eTag = $partList[$i]['eTag'];

            $partPath = 'chunk/' . $uploadId . '/' . $partNumber;

            $partETag = md5(Filesystem::disk('local')->read($partPath));

            if ($eTag != $partETag) {
                return $this->error('分片上传失败');
            }

            Filesystem::disk('local')->append($path, Filesystem::disk('local')->read($partPath));
        }

        Filesystem::disk('local')->deleteDir('chunk/' . $uploadId);

        return $this->success('上传成功', ['value' => $path]);
    }
}